package cn.yunhe.server;

import cn.yunhe.dao.IHouseDao;
import cn.yunhe.entity.House;
import cn.yunhe.util.PageUtil;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HouseDaoServer {


    private static final int CONDITION_NULL = 0;  //定义两个条件判断数字,0代表web页面未选择查询条件,1代表前端页面选择了查询条件
    private static final int CONDITION_NOT_NULL = 1;

    /**
     * 发布房屋信息方法
     *
     * @param house 房屋对象
     * @return 发布结果
     */
    public boolean issueHouseInfo(House house) {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IHouseDao houseDao = session.getMapper(IHouseDao.class);
        try {
            boolean flag = houseDao.issueHouseInfo(house); //执行房屋信息发布方法
            if (flag) { //如果发布结果为true则提交事务
                session.commit();
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            session.rollback();
            e.printStackTrace();
            return false;
        }finally {
            if (session != null){
                session.close();
            }
        }
    }

    /**
     * 房屋信息查询方法
     *
     * @param map      房屋信息集合
     * @param currPage 当前页面
     * @return 房屋信息对象集合
     */
    public List<House> queryHouse(Map<String, Object> map, int currPage) {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IHouseDao houseDao = session.getMapper(IHouseDao.class);
        Map<String, Object> mapSerch = new HashMap<String, Object>(); //定义一个集合封装查询条件
        //获取各个查询条件
        String title = (String) map.get("title");
        String price = (String) map.get("price");
        String province = (String) map.get("province");
        String city = (String) map.get("city");
        String district = (String) map.get("district");
        String type = (String) map.get("type");
        String area = (String) map.get("area");

        //若查询条件为空说明用户未选择该条件,将该条件标记为0,不为空则将该条件标记为1,同时存入该条件用于查询
        if (title == null) { //获取搜索标题
            mapSerch.put("titleNo", CONDITION_NULL);
        } else {
            mapSerch.put("titleNo", CONDITION_NOT_NULL);
            mapSerch.put("title", title);
        }

        if (price == null) { //获取房屋单价
            mapSerch.put("priceLowNo", CONDITION_NULL);
            mapSerch.put("priceHighNo", CONDITION_NULL);
        } else {  //将价格字符串拆分,获取最高价格和最低价格
            String[] prices = price.split("-");
            String priceLow = prices[0];
            String priceHigh = prices[1];
            mapSerch.put("priceLowNo", CONDITION_NOT_NULL);
            mapSerch.put("priceHighNo", CONDITION_NOT_NULL);
            mapSerch.put("priceLow", Double.parseDouble(priceLow));
            mapSerch.put("priceHigh", Double.parseDouble(priceHigh));
        }

        if (province == null) { //获取省份
            mapSerch.put("provinceNo", CONDITION_NULL);
        } else {
            mapSerch.put("provinceNo", CONDITION_NOT_NULL);
            mapSerch.put("province", province);
        }

        if (city == null) {  //获取城市
            mapSerch.put("cityNo", CONDITION_NULL);
        } else {
            mapSerch.put("cityNo", CONDITION_NOT_NULL);
            mapSerch.put("city", city);
        }

        if (district == null) { //获取区县
            mapSerch.put("districtNo", CONDITION_NULL);
        } else {
            mapSerch.put("districtNo", CONDITION_NOT_NULL);
            mapSerch.put("district", district);
        }

        if (type == null) { //获取房屋类型
            mapSerch.put("typeNo", CONDITION_NULL);
        } else {
            mapSerch.put("typeNo", CONDITION_NOT_NULL);
            mapSerch.put("tName", type);
        }
        if (area == null) {  //获取房屋面积
            mapSerch.put("areaLowNo", CONDITION_NULL);
            mapSerch.put("areaHighNo", CONDITION_NULL);

        } else { //将面积字符串拆分,获取最大价格和最小价格
            String[] areas = area.split("-");
            String areaLow = areas[0];
            String areaHigh = areas[1];
            mapSerch.put("areaLowNo", CONDITION_NOT_NULL);
            mapSerch.put("areaHighNo", CONDITION_NOT_NULL);
            mapSerch.put("areaLow", Double.parseDouble(areaLow));
            mapSerch.put("areaHigh", Double.parseDouble(areaHigh));
        }
        //封装页面编号和页面尺寸用户分页查询
        mapSerch.put("pageNo", (currPage - 1) * PageUtil.PAGE_SIZE);
        mapSerch.put("pageSize", PageUtil.PAGE_SIZE);
        try {
            return houseDao.queryHouse(mapSerch);
        }catch (Exception e){
            return null;
        }finally {
            if (session != null){
                session.close();
            }
        }
    }


    /**
     * 统计总行数
     *
     * @param map
     * @return
     */
    public int countRows(Map<String, Object> map) {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IHouseDao houseDao = session.getMapper(IHouseDao.class);
        Map<String, Object> mapCount = new HashMap<String, Object>(); //新建一个集合用于存放参数
        String price = (String) map.get("price"); //获取房屋价格
        String area = (String) map.get("area");  //获取房屋面积
        mapCount.put("title", map.get("title"));  //存放查询标题
        mapCount.put("province", map.get("province"));  //存放查询的省份
        mapCount.put("city", map.get("city"));  //存放城市
        mapCount.put("district", map.get("district")); //存放区县
        mapCount.put("type", map.get("type")); //存放房屋类型

        if (area != null) { //将面积字符串拆分,获取最大价格和最小价格
            String[] areas = area.split("-");
            String areaLow = areas[0];
            String areaHigh = areas[1];
            mapCount.put("areaLow", Double.parseDouble(areaLow));
            mapCount.put("areaHigh", Double.parseDouble(areaHigh));
        }

        if (price != null) { //将价格字符串拆分,获取最高价格和最低价格
            String[] prices = price.split("-");
            String priceLow = prices[0];
            String priceHigh = prices[1];
            mapCount.put("priceLow", Double.parseDouble(priceLow));
            mapCount.put("priceHigh", Double.parseDouble(priceHigh));
        }
        try {
            return houseDao.countRows(mapCount);
        }catch (Exception e){
            return 0;
        }finally {
            if (session != null){
                session.close();
            }
        }
    }


    /**
     * 获取所有房屋信息
     *
     * @return 房屋对象集合
     */
    public List<House> getAllHouse() {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IHouseDao houseDao = session.getMapper(IHouseDao.class);
        Map map = new HashMap();
        try {
            return houseDao.getAllHouse(map);
        }catch (Exception e){
            return null;
        }finally {
            if (session != null){
                session.close();
            }
        }
    }


    /**
     * 统计所有行数
     *
     * @return
     */
    public int countAllRows() {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IHouseDao houseDao = session.getMapper(IHouseDao.class);
        try {
            return houseDao.countAllRows();
        }catch (Exception e){
            return 0;
        }finally {
            if (session != null){
                session.close();
            }
        }
    }

    /**
     * 分页查询房屋信息
     *
     * @param pageNo 当前页码
     * @return
     */
    public List<House> getAllHouseInfo(int pageNo) {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IHouseDao houseDao = session.getMapper(IHouseDao.class);
        if (pageNo == 0) { //若页码初始化
            pageNo = 1;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageNo", (pageNo - 1) * PageUtil.PAGE_SIZE);
        map.put("pageSize", PageUtil.PAGE_SIZE);
        try {
            return houseDao.getAllHouse(map);
        }catch (Exception e){
            return null;
        }finally {
            if (session != null){
                session.close();
            }
        }
    }

    /**
     * 通过房屋id查询房屋对象
     *
     * @param hid 房屋id
     * @return
     */
    public House getHouseById(int hid) {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IHouseDao houseDao = session.getMapper(IHouseDao.class);
        try {
            return houseDao.getHouseById(hid);
        }catch (Exception e){
            return null;
        }finally {
            if (session != null){
                session.close();
            }
        }
    }

    /**
     * 通过关键字模糊查询房屋集合
     *
     * @param keyword 关键字
     * @return 房屋对象集合
     */
    public List<House> fuzzySerchHouse(String keyword) {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        IHouseDao houseDao = session.getMapper(IHouseDao.class);
        try {
            return houseDao.fuzzySerchHouse(keyword);
        }catch (Exception e){
            return null;
        }finally {
            if (session != null){
                session.close();
            }
        }
    }

}
